package com.qiaotongtianxia.loans.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.qiaotongtianxia.loans.R;

import java.lang.reflect.Field;

import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;

/**
 * 设置状态栏的工具类
 */

public class StatusBarUtils {
    /**
     * 设置状态栏的颜色
     * 此方法必须在setcontentview后调用
     * @param activity
     * @param colorResId
     * @param activityBackground
     */
    public static void setWindowStatusBarColor(Activity activity, int colorResId,@ColorRes int activityBackground) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
//                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//                window.setStatusBarColor(ContextCompat.getColor(activity,colorResId));
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );//SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);

                //此处优化沉浸式后的过度绘制-----------
                window.getDecorView().setBackgroundColor(ContextCompat.getColor(activity, android.R.color.transparent));
                ViewGroup contentView = (ViewGroup) window.getDecorView().findViewById(android.R.id.content);
//                contentView.setBackgroundColor(ContextCompat.getColor(activity,colorResId));
                setContentViewBg(activity,contentView,colorResId,activityBackground);
                contentView.getChildAt(0).setFitsSystemWindows(true);

            }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

                //此处会设置布局的根布局的背景色，xml中根布局设置背景色此方法将失效
                //此处优化沉浸式后的过度绘制-----------
                activity.getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(activity, android.R.color.transparent));
                ViewGroup contentView = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);
//                contentView.setBackgroundColor(ContextCompat.getColor(activity,colorResId));
                setContentViewBg(activity,contentView,colorResId,activityBackground);
                contentView.getChildAt(0).setFitsSystemWindows(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param activity
     * @param activityBackground
     */
    public static void setWindowStatusBarWhite(Activity activity,@ColorRes int activityBackground) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Window window = activity.getWindow();
//                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//                window.setStatusBarColor(ContextCompat.getColor(activity,colorResId));
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE|SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );//SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);

                //此处优化沉浸式后的过度绘制-----------
                window.getDecorView().setBackgroundColor(ContextCompat.getColor(activity, android.R.color.transparent));
                ViewGroup contentView = (ViewGroup) window.getDecorView().findViewById(android.R.id.content);
//                contentView.setBackgroundColor(ContextCompat.getColor(activity,colorResId));
                setContentViewBg(activity,contentView,R.color.white,activityBackground);
                contentView.getChildAt(0).setFitsSystemWindows(true);

            }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

                //此处会设置布局的根布局的背景色，xml中根布局设置背景色此方法将失效
                //此处优化沉浸式后的过度绘制-----------
                activity.getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(activity, android.R.color.transparent));
                ViewGroup contentView = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);
//                contentView.setBackgroundColor(ContextCompat.getColor(activity,colorResId));
                setContentViewBg(activity,contentView, R.color.gray,activityBackground);
                contentView.getChildAt(0).setFitsSystemWindows(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 只设置状态栏透明
     * @param activity
     */
    public static void setWindowStatusBarTrans(Activity activity){
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();

                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);

            }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 为contentView设置一个drawble，上部分100px的高度是状态栏要设置的颜色，下部分是白色的
     * @param activity
     * @param contentView
     * @param colorResId
     * @param activityBackground
     */
    private static void setContentViewBg(Activity activity, ViewGroup contentView, @ColorRes int colorResId,@ColorRes int activityBackground){

        int statusHeight = getStatusBarHeight(activity);

        Drawable[] drawables=new Drawable[2];
        drawables[0]=new PaintDrawable(ContextCompat.getColor(activity,colorResId));
        drawables[1]=new PaintDrawable(ContextCompat.getColor(activity, activityBackground));
        LayerDrawable layer=new LayerDrawable(drawables);//防止过度绘制
        layer.setLayerInset(0, 0, 0, 0, CommonUtils.getScreenHeight(activity) - statusHeight);
        layer.setLayerInset(1, 0, statusHeight, 0, 0);
        contentView.setBackground(layer);
    }

    public static void setWindowStatusBarColor(Dialog dialog, int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = dialog.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(dialog.getContext().getResources().getColor(colorResId));

                //底部导航栏
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static int getStatusBarHeight(Context context) {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return context.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
